﻿/*

/-----------------------------------------------\
|          CODE by Kirati Isarapungpun          |
|       For North - Chiang Mai University       |
|       Software Engineering Project 2013       |
|                                               |
|  |-|>===================================<|-|  |
|                                               |
|       Course planning for Bachelor of         |
| Science degree in software engineering 2012   | 
|      Faculty of Science and Technology,       |
|        North - Chiang Mai University          |
|                                               |
\-----------------------------------------------/

/														\
	Plan	:	งานทุกอย่างที่เกี่ยวกับ การวางรายวิชาที่เปิดสอนในแต่ละเทอม
	เพิ่ม ลบ
\														/


*/

$(function()
{
	getplandata();
});

var loopYear = 2012,maxYear = 0

function getplandata()
{
	$('#plantable').html('');
	var loopTerm = 1;
	$.get('class/_reqplanmax.php',function(data){
		window.maxYaer = parseInt(data);
		while(loopYear < maxYaer+1){
			$('#plantable').html($('#plantable').html()+'<tr><th colspan="3">Year '+loopYear+'</th></tr><tr><th id="th1">Semster 1</th><th id="th2">Semster 2</th></tr><tr><td><table id="'+loopYear+'term1"></table></td><td><table id="'+loopYear+'term2"></table></td></tr><tr><th colspan="2" id="th3">Semster 3</th></tr><tr><td colspan="2"><table id="'+loopYear+'term3"></table></td></tr>');
			loopTerm = 1;
			while(loopTerm < 4){
				$('#'+loopYear+'term'+loopTerm).html('<tr><th>Subject Number</th><th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subject&nbsp;Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th>Credit</th><th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pre&nbsp;Subject&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th>Delete</th></tr>');
				loopTerm++;
			}
			loopYear++;
		}
	});
	
	$.getJSON('class/_reqplan.php',function(data)
	{
		$.each(data,function(key,val){
			$('#'+val['year']+'term'+val['term']).html($('#'+val['year']+'term'+val['term']).html()+'<tr id="data'+val['id']+'"><td id="snumber'+val['id']+'">'+val['sNumber']+'</td><td id="sName'+val['id']+'"><input type="hidden" id="subject'+val['id']+'" value="'+ val['sid']+'">'+ val['sName']+'</td><td id="sCredit'+val['id']+'">'+val['sCredit']+'</td><td id="sPre'+val['id']+'">'+val['sPreName']+'</td><td><li class="ui-state-default ui-corner-all" id="delbutton'+val['id']+'" title="Delete" onClick="plandelcon('+val['id']+');"><span class="ui-icon ui-icon-trash"></span></li></td></tr>');
			//$('#snumber'+val['id']).button().css({'cursor': 'default','width': '30'});
			//$('#sName'+val['id']).button().css({'cursor': 'default','width': '200'});
			//$('#sCredit'+val['id']).button().css({'cursor': 'default','width': '30'});
			//$('#sPre'+val['id']).button().css({'cursor': 'default','width': '200'});
			//$('#editbutton'+val['id']).css({'cursor': 'pointer','width': '50'});
			$('#delbutton'+val['id']).button().css({'cursor': 'pointer','width': '50'});
		});
		
		window.loopYear = 2012;
		while(loopYear < window.maxYaer+1){
			loopTerm = 1;
			while(loopTerm < 4){
				$('#'+loopYear+'term'+loopTerm).html($('#'+loopYear+'term'+loopTerm).html()+'<tr><td id="'+loopYear+'addterm'+loopTerm+'" colspan="6" onClick="planaddbutton('+loopYear+','+loopTerm+');">ADD</td></tr>');
				$('#'+loopYear+'addterm'+loopTerm).button().css({'width': '500'});
				loopTerm++;
			}
			loopYear++;
		}
		$('#plantable').html($('#plantable').html()+'<tr><td colspan="6" id="addYear"><div title="Add another year" onClick="addYear();"> ADD Year </td></tr>');
		$('#addYear').button();
	});
	
}

function addYear()
{
	var loopYear = window.loopYear;
	$('#plantable').html($('#plantable').html()+'<tr><th colspan="3">Year '+loopYear+'</th></tr><tr><th id="th1">Term1</th><th id="th2">Term2</th></tr><tr><td><table id="'+loopYear+'term1"></table></td><td><table id="'+loopYear+'term2"></table></td></tr><th colspan="2" id="th3">Term3</th><tr><tr><td colspan="2"><table id="'+loopYear+'term3"></table></td></tr>');
	var loopTerm = 1;
	while(loopTerm < 4){
		$('#'+loopYear+'term'+loopTerm).html('<tr><th><input type="hidden" id="count'+loopYear+'_'+loopTerm+'" value="0">Subject Number</th><th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subject&nbsp;Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th>Credit</th><th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pre&nbsp;Subject&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th>Delete</th></tr>');
		$('#'+loopYear+'term'+loopTerm).html($('#'+loopYear+'term'+loopTerm).html()+'<tr><td id="'+loopYear+'addterm'+loopTerm+'" colspan="7" onClick="planaddbutton('+loopYear+','+loopTerm+');">ADD</td></tr>');
		$('#'+loopYear+'addterm'+loopTerm).button().css({'width': '500'});
		loopTerm++;
	}
	$('#addYear').hide();
}

function planaddbutton(year,term)
{
	$.get('ui/planselectdialog.php',function(data)
	{
	$('#plandialog').html(data);
	pdepselect('#pselectdepadd');
	$('#pselectdepadd').change(function () {
		$('#pselectdepadd option:selected').each(function () {
			pgopselect('#pselectgopadd',$('#pselectdepadd').val());
		});
	}).trigger('change');
	$('#pselectgopadd').change(function () {
		$('#pselectgopadd option:selected').each(function () {
			psubselect('#pselectsubadd',$('#pselectgopadd').val());
		});
	}).trigger('change');
	$('#pselectsubadd').change(function () {
		$('#pselectsubadd option:selected').each(function () {
			$.getJSON('class/_reqonesub.php',{ sid : $('#pselectsubadd').val()},function(data)
			{
				$.each(data,function(key,val){
					$('#pdetail').html('Credit :'+val['credit']+'<br> Pre Subject : '+val['sName']);
				});
			});
		});
	}).trigger('change');
	$('#plandialog').dialog({ 'title':'Add','width':'500',buttons: [{text: 'Refresh',click: function()
		{ 
			$( this ).dialog( 'close' ); 
			planaddbutton(year,term);
		}},{ text: 'ADD', click: function() 
		{ 
			planadd(year,term);
			$( this ).dialog( 'close' ); 
		}},{ text: 'Cancel', click: function() { $( this ).dialog( 'close' ); } }]});
	});
}

function pdepselect(where)
{
		$(where).html('<option value=""></option>');
		$.getJSON('class/_reqdep.php',function(data)
		{
			$.each(data,function(key,val){
			$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['number']+' - '+val['name']+'</option>');
			});
		});
}

function pgopselect(where,id)
{
		$(where).html('<option value=""></option>');
		$.getJSON('class/_reqgop.php',function(data)
		{
			$.each(data,function(key,val){
				if(val['did'] == id)
				{
					$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['number']+' - '+val['name']+'</option>');
				}
			});
		})
}

function psubselect(where,id)
{
		$(where).html('<option value=""></option>');
		$.getJSON('class/_reqsub.php',function(data)
		{
			$.each(data,function(key,val){
			if(val['gid'] == id)
			{
				$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['number']+' - '+val['name']+'</option>');
			}
			});
		});
}

function planadd(year,term)
{
	if($('#pselectdepadd').val() == '')
	{
		$('#dialog').html('Plase enter Deparment');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#pselectgopadd').val() == '')
	{
		$('#dialog').html('Plase enter Subject Group');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#pselectsubadd').val() == '')
	{
		$('#dialog').html('Plase enter Subject');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else
	{
		$.get('class/_reqplaninsert.php',{'sid':$('#pselectsubadd').val(),'year':year,'term':term},function(data)
		{
			$('#dialog').html('Insert Success');
			$('#dialog').dialog({'title':'Success'});
			$('#dialog').dialog('option','resizable',false);
			$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
			getplandata();
		});
	}
}

function planeditbutton(id,year,term)
{
	$.get('ui/planeditdialog.php',function(data)
	{
	$('#planeditdialog').html(data);
	pdepselect('#pselectdepedit');
	$('#pselectdepedit').change(function () {
		$('#pselectdepedit option:selected').each(function () {
			pgopselect('#pselectgopedit',$('#pselectdepedit').val());
		});
	}).trigger('change');
	$('#pselectgopedit').change(function () {
		$('#pselectgopedit option:selected').each(function () {
			psubselect('#pselectsubedit',$('#pselectgopedit').val());
		});
	}).trigger('change');
	$('#planeditdialog').dialog({ 'title':'Edit','width':'500',buttons: [{text: 'Refresh',click: function()
		{ 
			$( this ).dialog( 'close' ); 
			planeditbutton(id,year,term);
		}},{ text: 'Edit', click: function() 
		{ 
			planupdate(id,year,term);
			$( this ).dialog( 'close' ); 
		}},{ text: 'Cancel', click: function() { $( this ).dialog( 'close' ); } }]});
	});
}

function planupdate(id,year,term)
{
	if($('#pselectsubedit'+id).val() == '')
	{
		$('#dialog').html('Plase choise Subject');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else
	{
		$.get('class/_reqplanupdate.php',{'id':id,'sid':$('#pselectsubedit').val(),'year':year,'term':term},function(data)
		{
			$('#dialog').html('Edit Success');
			$('#dialog').dialog({'title':'Success'});
			$('#dialog').dialog('option','resizable',false);
			$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
			getplandata();
		});
	}
}

function plandelcon(id)
{
	$('#delcon').html('You want to Delete this?');
		$('#delcon').dialog({ buttons: [{text: 'Delete',click: function()
		{ 
			plandeldata(id);
			$( this ).dialog('close'); 
		}},{ text: 'Cancel', click: function() { $( this ).dialog( 'close' ); } }]});
		$('#delcon').dialog({'title':'Confirm Delete'});
		$('#delcon').dialog('option','resizable',false);
}

function plandeldata(id)
{
	$.get('class/_reqplandel.php',{'id':id},function(data)
	{
		$('#dialog').html('Delete Success');
		$('#dialog').dialog({'title':'Success'});
		$('#dialog').dialog('option','resizable',false);
		$('#data'+id).hide();
	});
}